<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    <!--引入Vue-->
    <script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
    <div class="root">
      <h2>今天天气很{{info}}</h2>
      <button @click="changeWeather">切换天气</button>
      <hr/>
      <h3>a的值是:{{numbers.a}}</h3>
      <button @click="numbers.a++">点我让a+1</button>
      <hr/>
      <h3>b的值是:{{numbers.b}}</h3>
      <button @click="numbers.b++">点我让b+1</button>
    </div>
</body>

<script type="text/javascript">
  let vue = new Vue({
    el: '.root',
    data: {
      isHot: true,
      numbers: {
        a: 1,
        b: 1
      }
    },
    /**
     * 监视属性变化
     */
    watch:{
      isHot: {
        /**
         * handler
         * 当isHot属性值发生改变时,handler方法会被调用
         */
        handler(newValue,oldValue){
          //在这里就能得到变化前的值和变化后的值
          console.log('isHot被修改了,'+newValue+','+oldValue)
        },
        /**
         * 为true,初始化时会调用一次handler方法
         */
        // immediate: true
      },
      /**
       * 监视多级结构中某个属性的变化
       */
      // 'numbers.a':{
      //   handler(newValue,oldValue){
      //     console.log('a变化了:'+newValue)
      //   }
      // },
      /**
       * 监视多级结构中所有属性的变化
       */
      numbers: {
        //深度监视,默认是false
        deep: true,
        handler(newValue, oldValue) {
          console.log('numbers变化了:' + newValue)
        }
      }
    },
    computed:{
      info(){
        return this.isHot ? '炎热' : '凉爽'
      }
    },
    methods:{
      changeWeather(){
        this.isHot = !this.isHot
      }
    }
  })
</script>
</html>
